package com.mfw.mq;

import org.apache.rocketmq.client.annotation.RocketMQMessageListener;
import org.apache.rocketmq.client.apis.consumer.ConsumeResult;
import org.apache.rocketmq.client.apis.message.MessageView;
import org.apache.rocketmq.client.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

@Service
@RocketMQMessageListener(consumerGroup = "delay_g", topic = "dddii", tag = "*")
public class RocketMQDelayConsumer implements RocketMQListener {
    private static Logger logger = LoggerFactory.getLogger(RocketMQDelayConsumer.class);
    @Override
    public ConsumeResult consume(MessageView messageView) {
        // 从 MessageView 中获取 ByteBuffer
        ByteBuffer byteBuffer = messageView.getBody();

        // 转换 ByteBuffer 为字节数组
        byte[] body = new byte[byteBuffer.remaining()];
        byteBuffer.get(body);

        // 处理字节数组，例如转换为字符串
        String messageBody = new String(body, StandardCharsets.UTF_8);

        logger.info(System.currentTimeMillis() +"收到延时消息 内容：" + messageBody);

        return ConsumeResult.SUCCESS;
    }
}
